<?php
namespace app\techn_api\controller\v1;
use think\Db;
use think\Request;


class Move extends Base
{
	public function _initialize()
	{
        parent::_initialize();
        $this->IsLogin(); //验证登录
        $this->IsTechn(); //验证技师


    }

    public function move_sheng(){
        $list = Db::name('proxy')->where('is_delete_time',0)->group('sheng')->column('sheng');
        $names = [];
        $return = [];
        if (!empty($list)){
            foreach ($list as $key=>$value){
                $name = Db::name('area')->where('id',$value)->column('name','id');
                $names[] = $name;
            }
            foreach ($names as $kk => $vv){
                foreach ($vv as $kkk => $vvv){
                    $return[$kk]['typeid'] = $kkk;
                    $return[$kk]['type'] = $vvv;
                }
            }
        }
        $echo['code'] = 0;
        $echo['msg'] = '获取成功';
        $echo['data'] = $return;
        echo json_encode($echo);
    }

    public function sheng(Request $request){
        $param = $request->param();
       if (!empty($param['check_sheng'])){

           $list = Db::name('proxy')->where('is_delete_time',0)->group('shi')->column('shi');
           $shi = Db::name('area')
               ->where('id','in',$list)
               ->where('pid',$param['check_sheng'])
               ->where('lev',2)
               ->column('name','id');

           if (!empty($shi)){
               $i = 0;
               foreach ($shi as $kk => $vv){
                   $return[$i]['typeid'] = $kk;
                   $return[$i]['type'] = $vv;
                   $i++;
               }
               $echo['code'] = 0;
               $echo['msg'] = '获取成功';
               $echo['data'] = $return;
               echo json_encode($echo);
           }
       }
    }

    public function shi(Request $request){
        $param = $request->param();
        if (!empty($param['check_shi'])){
            $proxy = Db::name('proxy')->where('shi',$param['check_shi'])->column('qu');
            $proxy_list = implode(',',$proxy);
            $shi = Db::name('area')
                ->where('pid',$param['check_shi'])
                ->where('id','in',$proxy_list)
                ->where('lev',3)
                ->column('name','id');

            if (!empty($shi)){
                $i = 0;
                foreach ($shi as $kk => $vv){
                    $return[$i]['q_id'] = $kk;
                    $return[$i]['q_name'] = $vv;
                    $i++;
                }
                $echo['code'] = 0;
                $echo['msg'] = '获取成功';
                $echo['data'] = $return;
                echo json_encode($echo);
            }
        }
    }

    public function shenqing(Request $request){
        $param = $request->param();
        if (!empty($param)){
            $techn = $this->techn['id'];

            $shen_info = Db::name('techn_move')->where('techn_id',$techn)->where('status',0)->find();
            if (!empty($shen_info)){
                echo json_encode(['code'=>-1,'msg'=>'已在申请中']);
                exit;
            }
            $techninfo = Db::name('techn')->where('id',$techn)->find();
            if ($techn != 299){
                if ($techninfo['money'] >1){
                    echo json_encode(['code'=>-1,'msg'=>'请将钱包金额提现后再申请']);
                    exit;
                }
            }
            if ($param['sheng'] == $techninfo['province'] && $param['shi'] == $techninfo['city'] && $param['qu'] == $techninfo['county']){
                echo json_encode(['code'=>-1,'msg'=>'您已经在该城市工作']);
                exit;
            }
            $param['techn_id'] = $techn;
            $param['old_sheng'] = $techninfo['province'];
            $param['old_shi'] = $techninfo['city'];
            $param['old_qu'] = $techninfo['county'];
            $param['add_time'] = time();
            $re = Db::name('techn_move')->insertGetId($param);
            if ($re){
                echo json_encode(['code'=>0,'msg'=>'申请成功，请联系管理员审核']);
            }
        }
    }

    public function lahei(Request $request){
        $param = $request->param();
        $order_info = Db::name('order')->where('id',$param['order_id'])->find();
        $techn = $this->techn;

        $not_show = Db::name('techn')->where('id',$techn['id'])->value('not_show');
        if (!empty($not_show)){
            $not_list = explode(',',$not_show);
            if (in_array($order_info['user_id'],$not_list)){
                echo json_encode(['code'=>0,'msg'=>'拉黑成功']);
                exit();
            }else{
                array_push($not_list,$order_info['user_id']);
                $up = implode(',',$not_list);
            }
        }else{
            $up = $order_info['user_id'];
        }
        $result = Db::name('techn')->where('id',$techn['id'])->update(['not_show'=>$up]);
        if ($result){
            echo json_encode(['code'=>0,'msg'=>'拉黑成功']);
            exit();
        }else{
            echo json_encode(['code'=>-1,'msg'=>'拉黑失败,请重试']);
            exit();
        }
    }


}